#include "tlist.h"

TList::TList( void )
{

}

TList::~TList( void )
{
	Init();
}

void TList::Init( void )
{
	_top._prior = _top._next = &_top;
}

void TList::AddObj( TListObj* obj )
{
	TListObj* last = _top._prior;
	last->_next = obj;
	obj->_prior = last;
	obj->_next = &_top;
	_top._prior = obj;
}

void TList::DelObj( TListObj* obj )
{
	obj->_prior->_next = obj->_next;
	obj->_next->_prior = obj->_prior;
	obj->_next = obj->_prior = 0;
}

TListObj* TList::TopObj( void )
{
	return &_top == _top._next ? 0 : _top._next;
}

TListObj* TList::NextObj( TListObj* obj )
{
	return &_top == obj->_next ? 0 : obj->_next;
}

bool TList::IsEmpty( void )
{
	return _top._prior == _top._next;
}

void TList::MoveList( TList* fromList )
{
	if( !fromList->IsEmpty() )
	{
		if( IsEmpty() )
		{
			_top = fromList->_top;
			_top._next->_prior = &_top;
		}
		else
		{
			_top._prior->_next = fromList->_top._next;
			fromList->_top._next->_prior = _top._prior;
			_top._prior = fromList->_top._prior;
			fromList->_top._prior->_next = &_top;
		}
		fromList->Init();
	}
}